package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.Adapter.FoodAdapter;
import cn.dnui_yxk801.dnuifood_16110100801.Adapter.ShopAdapter;
import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CollectBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.FoodBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.ShopModel;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

import static cn.dnui_yxk801.dnuifood_16110100801.MainActivity.userid;

public class FoodListActivity extends BaseActivity implements OnRetrofitListener, View.OnClickListener {
    private String shopid;
    private String shopname;
    private Button btn_collect;
    private ImageView im_collect;
    //private View view;//定义view用来设置fragment的layout
    private RecyclerView recyclerView;//定义RecyclerView
    //定义以goodsentity实体类为对象的数据集合
    //private ArrayList<FoodBean> foodsList = new ArrayList<>();
    //自定义recyclerveiw的适配器
    private FoodAdapter foodAdapter;
    //private List<FoodBean> list;

   public FoodListActivity()
    {

    }

    @Override
    void initViews() {
        layout_file=R.layout.foodlist;
        setLayout(layout_file);
        btn_collect=findViewById(R.id.fl_btn_collect);
        im_collect=findViewById(R.id.fl_iv_collect);
        recyclerView = findViewById(R.id.rv_food);
        //创建默认的线性
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        // 设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true); //创建并设置Adapter
        //list = getData();
        initData();
    }

    @Override
    void initEvents() {
        btn_collect.setOnClickListener(this);
    }
    public void onClick(View view) {
        UserModel userModel=new UserModel();
        userModel.userCollectShop(userid,shopid,this);
    }

    @Override
    void initDatas() {

    }

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
       // setContentView(R.layout.foodlist);
        //initRecyclerView();
    }

//    private void initRecyclerView() {
//
//        //创建默认的线性
//        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
//        // 设置布局管理器
//        recyclerView.setLayoutManager(layoutManager);
//        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
//        recyclerView.setHasFixedSize(true); //创建并设置Adapter
//        //list = getData();
//        initData();
//
//    }


    void initData() {
       Intent intent=getIntent();
       shopid=intent.getStringExtra("shopid");
       shopname=intent.getStringExtra("shopname");
       //shopid="1";
        ShopModel shopModel = new ShopModel();
        shopModel.getFoodByShop(shopid,this);
        shopModel.isCollected(userid,shopid,"0",this);

    }



    @Override
    public void onSuccess(Object object, int flag) {
       if (flag==1){
           List foods=(List)object;
           foodAdapter = new FoodAdapter(this,shopname, foods);
           recyclerView.setAdapter(foodAdapter);
       }
       if (flag==2){

            if (btn_collect.getText().toString().equals("收藏")){
                im_collect.setImageResource(android.R.drawable.btn_star_big_on);
                btn_collect.setText("取消收藏");
                Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
            }
            else{
                im_collect.setImageResource(android.R.drawable.btn_star_big_off);
                btn_collect.setText("收藏");
                Toast.makeText(FoodListActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
            }
       }
       if (flag==7){
           CollectBean collectBean=(CollectBean)object;
           if ("1".equals(((CollectBean) object).getCollected())){
               im_collect.setImageResource(android.R.drawable.btn_star_big_on);
               btn_collect.setText("取消收藏");
           }
           else{
               im_collect.setImageResource(android.R.drawable.btn_star_big_off);
               btn_collect.setText("收藏");
           }
       }

    }



    @Override
    public void onFalure(String msg) {

    }



}
